/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.SanPham;
import pojo.ThuongHieu;
import util.HibernateUtil;

/**
 *
 * @author KhanhNhac
 */
public class ThuongHieuDAO {
    public static List<ThuongHieu> layDanhSach() {
        List<ThuongHieu> ds;

        Session ses = HibernateUtil.getSessionFactory().openSession();
        String hql = "select s from ThuongHieu s order by s.tenThuongHieu asc";
        Query query = ses.createQuery(hql);
        ds = query.list();
        ses.close();
        return ds;
    }

    public static ThuongHieu layThongTinThuongHieu(int maThuongHieu) {
        ThuongHieu dm = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            dm = (ThuongHieu) session.get(ThuongHieu.class, maThuongHieu);
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return dm;
    }

    public static int tinhSoLuongSanPhamThuocThuongHieu(String maThuongHieu) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "select count(*) as SoLuongSanPham from SanPham s where s.ThuongHieu.maThuongHieu = '"+maThuongHieu+"'";
        Query query = session.createQuery(hql);
        Object res = query.uniqueResult();
        int soLuong = Integer.parseInt(res.toString());
        session.close();
        return soLuong;
    }

    public static int tinhsoLuong(int maThuongHieu){
        Session session=HibernateUtil.getSessionFactory().openSession();
        List<SanPham> ds;
        String hql="Select s from SanPham where s.thuongHieu.maThuongHieu =:maTH";
        Query query=session.createQuery(hql);
        query.setInteger("maTH",maThuongHieu);
        ds=query.list();
        return ds.size();
    }
    
    public static int tinhSoLuong(int maThuongHieu){
        int sl=0;
        Session session = HibernateUtil.getSessionFactory().openSession();
      //  String hql=String.format("SELECT COUNT(*) AS SOLUONG FROM SACH WHERE MADANHMUC='%s'",maDanhMuc);
       String hql= "select count(*) as soluongsanpham from SanPham s where s.thuongHieu.maThuongHieu =:maThuongHieu";
         Query query=session.createQuery(hql);
        query.setInteger("maThuongHieu", maThuongHieu);
         try { 

            Object obj=query.uniqueResult();
            sl=Integer.parseInt(obj.toString());
        } catch (HibernateException ex) { 
            //Log the exception 
            System.err.println(ex); 
        } finally { 
            session.close(); 
        } 
        return sl; 
    }
    
    public static int tinhSoLuongTrang(int mdm, int soLuongSanPhamTrenTrang) {
        int soTrang;
        int sl = tinhSoLuong(mdm);
        if (soLuongSanPhamTrenTrang != -1) {
            soTrang = sl / soLuongSanPhamTrenTrang;
            if (sl % soLuongSanPhamTrenTrang != 0) {
                soTrang++;
            }
        } else {
            soTrang = 1;
        }
        return soTrang;
    }
}
